package io.github.eric.urpc.standalone.client;

import io.github.eric.urpc.client.Client;
import io.github.eric.urpc.core.rpc.RpcMethod;
import io.github.eric.urpc.core.rpc.RpcController;
import io.github.eric.urpc.standalone.common.*;
import io.github.eric.urpc.standalone.common.msg.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClientRpcController implements RpcController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ClientRpcController.class);

    private String userId;
    private String userName;
    private Client client;
    private MobileDevice mobileDevice;

    @RpcMethod
    public void onTestMessage(TestMessage testMessage) {
        LOGGER.info("client recv a test msg");
    }

    @RpcMethod
    public void onPongMessage(PongMessage pongMessage) {
        LOGGER.info("client recv a pong msg");
    }

    @RpcMethod
    public void onIMMessageResult(IMMessageResult imMessageResult) {
//        LOGGER.info("[系统消息]消息{}的发送结果: {}", imMessageResult.getMessageId(), imMessageResult.getMessageStatus());
    }

    @RpcMethod
    public IMMessageResult onIMMessage(IMMessage imMessage) {
        if (!imMessage.getToUserId().equals(userId)) {
            LOGGER.error("收到一条不应该发给我的消息, 发送人id {}, 接收人id {}, 消息id {}, 消息内容 {}",
                    imMessage.getFromUserId(), imMessage.getToUserId(),
                    imMessage.getMessageId(), imMessage.getContent());
        }
//        LOGGER.info("收到{}的消息: {}", imMessage.getFromUserId(), imMessage.getContent());
        IMMessageResult result = new IMMessageResult();
        result.setMessageId(imMessage.getMessageId());
        result.setFromUserId(imMessage.getFromUserId());
        result.setMessageStatus(MessageStatus.RECV_COMPLETED);
        return result;
    }

    @RpcMethod
    public void onPushUsersStart(PushUsersStart pushUsersStart) {
        mobileDevice.getFriendManager().startNewFriendsList();
    }

    @RpcMethod
    public void onPushUsers(PushUsers pushUsers) {
        mobileDevice.getFriendManager().addFriends(pushUsers.getUsers(), userId);
    }

    @RpcMethod
    public void onPushUsersFinish(PushUsersFinish pushUsersFinish) {
        mobileDevice.getFriendManager().finishNewFriendsList();
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    public void setClient(Client client) {
        this.client = client;
    }
    public void setMobileDevice(MobileDevice mobileDevice) {
        this.mobileDevice = mobileDevice;
    }
}
